package com.hero.part.boot.config;

import org.pac4j.cas.client.rest.CasRestBasicAuthClient;
import org.pac4j.cas.client.rest.CasRestFormClient;
import org.pac4j.cas.config.CasConfiguration;
import org.pac4j.core.authorization.authorizer.Authorizer;
import org.pac4j.core.client.Client;
import org.pac4j.core.client.Clients;
import org.pac4j.core.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuez
 * @date 2018/3/5
 */
@Configuration
public class Pac4jConfig extends WebMvcConfigurationSupport{

	@Override
	protected void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor();

		super.addInterceptors(registry);
	}

	/**
	 * 客户端
	 * @return
	 */
	@Bean
	public Clients clients(){

		List<Client> clientList = new ArrayList<>();

		CasConfiguration casConfiguration = new CasConfiguration();
		casConfiguration.setLoginUrl("https://casserverpac4j.herokuapp.com/login");
//		casConfiguration.setAcceptAnyProxy();
//		casConfiguration.setAllowedProxyChains();
//		casConfiguration.setDefaultTicketValidator();
//		casConfiguration.setEncoding();
//		casConfiguration.setGateway();
//		casConfiguration.setLogoutHandler();
//		casConfiguration.setPrefixUrl();
//		casConfiguration.setProtocol();
//		casConfiguration.setProxyReceptor();
//		casConfiguration.setRenew();
//		casConfiguration.setRestUrl();
//		casConfiguration.setTimeTolerance();
//		casConfiguration.setUrlResolver();


		//Cas 直接客户端
		CasRestBasicAuthClient casBasicClient = new CasRestBasicAuthClient();
		clientList.add(casBasicClient);
		casBasicClient.setHeaderName("Authorization");
		casBasicClient.setPrefixHeader("Basic ");
		casBasicClient.setConfiguration(casConfiguration);
//		casBasicClient.setProfileCreator();
//		casBasicClient.setAuthorizationGenerators();
//		casBasicClient.setCredentialsExtractor();
//		casBasicClient.setAuthenticator();


		CasRestFormClient casFromClient = new CasRestFormClient();
		clientList.add(casFromClient);
		casFromClient.setPasswordParameter("p");
		casFromClient.setUsernameParameter("u");
		casFromClient.setConfiguration(casConfiguration);


		Clients clients = new Clients();
		clients.setCallbackUrl("");
		clients.setClients();
//		clients.setAjaxRequestResolver();
		//全局的,会添加到每个Client中
//		clients.setAuthorizationGenerator();
		//回调url中clientName的变量名称
//		clients.setClientNameParameter();
//		clients.setDefaultClient();
		//当client的UrlResolver为空或为默认时,使用这个
//		clients.setUrlResolver();

		return clients;
	}

	/**
	 * 权限控制器
	 * @return
	 */
	@Bean
	public Map<String, Authorizer> authorizers(){
		Map<String, Authorizer> authorizerMap = new HashMap<>();

//		authorizerMap.put();

		return authorizerMap;
	}

	/**
	 * 配置
	 * @param clients
	 * @param authorizers
	 * @return
	 */
	@Bean
	public Config config(Clients clients,Map<String, Authorizer> authorizers){
		Config config = new Config(clients,authorizers);
//		config.setCallbackLogic();
//		config.setLogoutLogic();
//		config.setSecurityLogic();
//		config.setHttpActionAdapter();
//		config.setMatchers();
//		config.setProfileManagerFactory();
//		config.setSessionStore();
		return config;
	}


}
